const fs = require('fs')
const baseUrl = 'http://10.242.31.16:8999/hr/'
const token = `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzY3OTE0MTAsInVzZXJuYW1lIjoid3V4eTE2MyJ9.gw7vWgS9lIHdZefkD2xj0zdhE5lCAJj5-2nscs0Xkf4`
const baseUrl2 = 'http://132.120.204.17:9999/hr/'
const token2 = `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzY3OTEyMzIsInVzZXJuYW1lIjoid3V4eTE2MyJ9.faOllNPwjiMW0Tjsj04mLAJyZzxPtBEqIALtnKfTjxU`
const myFetch = (baseUrl, token, apiInfo, env) => {
    const body = { ...apiInfo.body }
    Object.keys(body).forEach(key => {
        body[key] = env[key] || body[key]
    })
    return fetch(baseUrl + apiInfo.url, {
        "headers": {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json;charset=UTF-8",
            "x-access-token": token,
            // "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzY3OTE0MTAsInVzZXJuYW1lIjoid3V4eTE2MyJ9.gw7vWgS9lIHdZefkD2xj0zdhE5lCAJj5-2nscs0Xkf4",
            "x-requested-with": "XMLHttpRequest"
        },
        "referrer": "http://10.242.31.16:8999/hrMonthPerspective/Index5",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": JSON.stringify(body),
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    }).then(res => res.text())
}

const apisInfo = [
    {
        "url": "monthlyPerspective/getMatchingTrend",
        name: '在线率趋势',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getUnemployedReason",
        name: '不在线原因',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getInserviceAnalysis",
        name: '在册在线分析',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getActivityTrend",
        name: '活跃度趋势',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getPostComposition",
        name: '在线构成 ',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getAverageAge",
        name: '平均年龄',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getAgeDistribution",
        name: '年龄分布',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getEducationalComposition",
        name: '学历分布',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getUnemployedTrend",
        name: '不在线人员趋势',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getReasonAnalysis",
        name: '作业透视',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getKpiTrend",
        name: '关键指标趋势',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getEfficiencyRank",
        name: '岗位人员效率排行',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "page": "1",
            "order": "RANK1",
            "sort": "ASC"
        }
    },
    {
        "url": "monthlyPerspective/getAvgCapacity",
        name: '人均积分趋势',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "cities": [
                "188",
                "720"
            ]
        }
    },
    {
        "url": "monthlyPerspective/getWLXWorkDetail",
        name: '作业透视',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5"
        }
    },
    {
        "url": "monthlyPerspective/getORGNumByKind",
        name: '作业透视',
        "body": {
            "acctDate": "202208",
            "orgId": "OG0619",
            "gw": "204",
            "profLine": "5",
            "gwKind": "601"
        }
    },
    // {
    //     "url": "monthlyPerspective/getWorkOrderDetail",
    //     "body": {
    //         "acctDate": "202208",
    //         "orgId": "OG0619",
    //         "gw": "204",
    //         "profLine": "5",
    //         "gwKind": "601",
    //         "page": 1,
    //         "size": 10,
    //         "operation_type": 1
    //     }
    // }
].map((it, idx) => (it.code = idx, it))
const sort = `
广东省分公司
广州
深圳
东莞
佛山
中山
珠海
江门
肇庆
惠州
汕头
潮州
揭阳
汕尾
湛江
茂名
阳江
云浮
韶关
清远
梅州
河源
`.split('\n').filter(Boolean).reduce((res, name, idx) => (res[name] = idx, res), {})


const org = [
    {
        "id": "000",
        "text": "广东省分公司"
    },
    {
        "id": "OG0614",
        "text": "广州"
    },
    {
        "id": "OG0619",
        "text": "深圳"
    },
    {
        "id": "OG0111",
        "text": "东莞"
    },
    {
        "id": "OG0618",
        "text": "佛山"
    },
    {
        "id": "OG0611",
        "text": "惠州"
    },
    {
        "id": "OG0113",
        "text": "珠海"
    },
    {
        "id": "OG0114",
        "text": "中山"
    },
    {
        "id": "OG0112",
        "text": "江门"
    },
    {
        "id": "OG0615",
        "text": "汕头"
    },
    {
        "id": "OG0607",
        "text": "湛江"
    },
    {
        "id": "OG0609",
        "text": "揭阳"
    },
    {
        "id": "OG0608",
        "text": "肇庆"
    },
    {
        "id": "OG0616",
        "text": "清远"
    },
    {
        "id": "OG0603",
        "text": "韶关"
    },
    {
        "id": "OG0605",
        "text": "潮州"
    },
    {
        "id": "OG0613",
        "text": "茂名"
    },
    {
        "id": "OG0617",
        "text": "河源"
    },
    {
        "id": "OG0612",
        "text": "汕尾"
    },
    {
        "id": "OG0606",
        "text": "阳江"
    },
    {
        "id": "OG0007",
        "text": "梅州"
    },
    {
        "id": "OG0604",
        "text": "云浮"
    },
    // {
    //     "id": "OG0610",
    //     "text": "省本部"
    // }
].map((it, idx) => (it.code = idx, it))
const sortedOrg = []
org.forEach(it => {
    sortedOrg[sort[it.text]] = it
})

// [202201 - 202301]
let date =
    Array(12).fill(1).map((_, idx) => (++idx).toString().padStart(2, '0')).map(it => `2${it}`)
        .concat('301').slice(4)
console.time('time')
console.timeLog('time')
const main = async () => {
    // date.length = 1
    // date = date.slice(3)
    while (date.length) {
        const acctDate = date.shift()
        let orgIdx = -1
        let orgMaxIdx = sortedOrg.length
        while (++orgIdx < orgMaxIdx) {
            const tasks = []
            const tags = []
            const it = sortedOrg[orgIdx]
            apisInfo.forEach(info => {
                tasks.push(Promise.allSettled([myFetch(baseUrl, token, info, { acctDate, orgId: it.id, gw: '207' }), myFetch(baseUrl2, token2, info, { acctDate, orgId: it.id, gw: '207' })]))
                tags.push(info.code)
                // tags.push(`${acctDate}","${it.text}","${info.name}","${info.url}`)
            })
            const res = await Promise.allSettled(tasks)
            // const log = []
            // const rightLog = []
            // res.forEach((item, idx) => {
            //     const res = item.value[0].value === item.value[1].value;
            //     if (res) return
            //     log.push(`"${acctDate}-${it.text}","${tags.join(',')}"`)
            //     // log.push([`"` + tags[idx], `${JSON.stringify((it.value[0].value))?.replace(/"/g, '')}`, `${JSON.stringify((it.value[1].value))?.replace(/"/g, '')}` + `"`].join('","'))
            //     // log.push([`"` + tags[idx], `${JSON.stringify(JSON.parse(it.value[0].value).result)?.replace(/"/g, '')}`, `${JSON.stringify(JSON.parse(it.value[1].value).result)?.replace(/"/g, '')}` + `"`].join('","'))

            // })
            const errTask = tags.filter((tag, idx) => {
                const [a, b] = res[idx].value
                return a.value !== b.value
            })
            const log = (`"${acctDate}-${it.text}","${errTask.join(',')}"\n`)

            console.log(`${acctDate}-${it.text} ×${errTask.length}/${apisInfo.length}`)
            console.timeLog('time')

            fs.writeFileSync('./res.log.csv',
                log,
                { flag: 'a' })
            // fs.writeFileSync('./resright.log.csv',
            //     rightLog.join('\n'),
            //     { flag: 'a' })
        }
    }
    console.timeEnd('time')
}
main()
// 